package com.group02.community.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.group02.community.entity.interaction.Interaction;
import com.group02.community.entity.interaction.ZyComment;
import com.group02.community.entity.interaction.ZyCommunityInteraction;
import com.group02.community.entity.vo.CommentVO;
import com.group02.community.entity.vo.InteractionVO;
import com.group02.community.service.InteractionService;
import com.group02.community.utils.ResponseResult;
import com.group02.community.utils.UUIDUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@Api
@RestController
public class InteractionController {

    @Autowired
    private InteractionService interactionService;

    /**
     * 获取分页社区互动信息+模糊查询
     * @return
     */
    @ApiOperation("获取所有互动表")
    @GetMapping("/interactions")
    public ResponseResult<IPage<InteractionVO>> getAllInteraction(Long current, Long size, Interaction interaction){
        IPage<InteractionVO> allInteraction = interactionService.getAllInteraction(current, size, interaction);
        return ResponseResult.ok(allInteraction);
    }

    /**
     * 通过交互id获取评论表信息
     * @return
     */
    @ApiOperation("通过InteractionId找到评论")
    @GetMapping("/interaction/{id}")
    public ResponseResult<List<CommentVO>> getCommentByInteractionId(@PathVariable String id){
        return ResponseResult.ok(interactionService.getCommentByInteractionId(id));
    }

    /**
     * 删除内容
     * 通过interactionId将有关的内容，以及评论的del——flag由0转为1删除状态
     * @return
     */
    @ApiOperation("删除所有内容以及相关评论")
    @DeleteMapping("/interactions/{interactionId}")
    public ResponseResult<String> deleteAllCommentAndInteraction(@PathVariable String interactionId){
        interactionService.updateCommentAndInteraction(interactionId);
        return ResponseResult.ok("删除成功");
    }

    @ApiOperation("删除评论")
    @DeleteMapping("/interaction/{commentId}")
    public ResponseResult<String> deleteCommentByCommentId(@PathVariable String commentId){
        interactionService.updateCommentByCommentId(commentId);
        return ResponseResult.ok("删除成功");
    }

    /**
     * 小程序的contnet接口
     * @return
     */
    @GetMapping("/uniApp/interactions/all")
    public ResponseResult<List<InteractionVO>> getAllInteraction(){
        List<InteractionVO> allInteraction = interactionService.getAllInteractions();
        return ResponseResult.ok(allInteraction);
    }

    @GetMapping("/uniApp/interaction/getComment")
    public ResponseResult<List<CommentVO>> getCommentByInteractionIdUniApp(String id){
        return ResponseResult.ok(interactionService.getCommentByInteractionId(id));
    }

    @PostMapping("/comment")
    public ResponseResult<String> sendComment(ZyComment zyComment){
        interactionService.saveComment(zyComment);
        return ResponseResult.ok("添加成功");
    }

    @PostMapping("/uniApp/interaction")
    public ResponseResult<String> add(ZyCommunityInteraction zyCommunityInteraction){
        zyCommunityInteraction.setInteractionId(String.valueOf(UUIDUtils.getId()));
        zyCommunityInteraction.setCommunityId(1338423709557272577L);
        zyCommunityInteraction.setCreateTime(LocalDateTime.now());
        zyCommunityInteraction.setDelFlag(0L);
        interactionService.save(zyCommunityInteraction);
        return ResponseResult.ok("添加成功");
    }
}
